package com.esd.controller.csair;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.esd.csair.CS;
import com.esd.csair.Csair;
import com.esd.csair.CsairImpl;
import com.esd.csair.CsairReturnValue;

/**
 * 
 * 支付模块
 * 
 * @author snailzhang
 * @date 2017年7月20日
 *
 */
@Controller
@RequestMapping(value = "cs")
public class CSMLoginController {

	private static Logger log = Logger.getLogger(CSMLoginController.class);

	/**
	 * 登录页面
	 *
	 * @author snailzhang
	 * @date 2017年7月20日
	 *
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public String login() {
		return "csair/login2";
	}

	/**
	 * 预加载登录页面
	 *
	 * @author snailzhang
	 * @date 2017年7月20日
	 *
	 * @param session
	 */
	@RequestMapping(value = "prelogin", method = RequestMethod.POST)
	@ResponseBody
	public void loadcslogin(HttpSession session) {
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		webClient.preLogin();
	}

	/**
	 * 登录
	 *
	 * @author snailzhang
	 * @date 2017年7月20日
	 *
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "login", method = RequestMethod.POST)
	@ResponseBody
	public CsairReturnValue cslogin(HttpServletRequest request, HttpSession session) {
		log.info("===============登     录====================");
		CsairImpl webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		webClient.preLogin();
		CsairReturnValue returnValue = webClient.login(username, password);
		if (returnValue.getState() == CS.SUCCESS) {
			session.setAttribute(Csair.USERNAME, returnValue.getUserName());
			session.setAttribute(Csair.LOGINTYPE, returnValue.getLoginType());
		}
		log.info("USERNAME=====>"+session.getAttribute(Csair.USERNAME));
		return returnValue;
	}

	/**
	 * 手机登录、找回密码、动态登录
	 */
	@RequestMapping(value = "findPassword", method = RequestMethod.GET)
	public String phone() {
		return "csair/findPassword2";
	}

	/**
	 * 动态密码登录 和 找回密码预加载
	 *
	 *
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "loadfindPassword", method = RequestMethod.POST)
	@ResponseBody
	public void prePhone(HttpSession session) {
		log.info("===============加载动态登录=================");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		webClient.preForgotPassword();
	}

	/**
	 * 找回密码发送验证码
	 * 
	 * @param username
	 * @return
	 */
	@RequestMapping(value = "getPassword", method = RequestMethod.POST)
	@ResponseBody
	public CsairReturnValue registerYzm(HttpServletRequest request, HttpSession session) {
		log.info("=================动态登录获取验证码=====================");
		String username = request.getParameter("username");
//		log.info("username------->" + username);
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		CsairReturnValue returnValue = webClient.forgotPassword(username);
		return returnValue;
	}

	/**
	 * 执行手机查询订单
	 * 
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "findpasswordlogin", method = RequestMethod.POST)
	@ResponseBody
	public CsairReturnValue phone(HttpSession session, HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
//		log.info("手机:[" + username + "]\n验证码:[" + password + "]");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		CsairReturnValue returnValue = webClient.forgotPasswordLogin(username, password);
		return returnValue;
	}

	/**
	 * 预加载修改密码页面
	 *
	 * @author snailzhang
	 * @date 2017年7月20日
	 *
	 * @param session
	 */
	@RequestMapping(value = "preChangePassword", method = RequestMethod.GET)
	public String loadEditPassword(HttpSession session) {
		log.info("=================加载修改密码===================");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		webClient.preModifyPassword();
		return "csair/changePassword2";
	}

	/**
	 * 修改密码
	 * 
	 * @param oldpassword
	 * @param newpassword
	 * @param newpasswordagain
	 * @return
	 */
	@RequestMapping(value = "changePassword", method = RequestMethod.POST)
	@ResponseBody
	public CsairReturnValue changePassword(HttpServletRequest request, HttpSession session) {
		log.info("==================执行修改密码=======================");
		String oldpassword = request.getParameter("oldpassword");
		String newpassword = request.getParameter("newpassword");
		String newpasswordagain = request.getParameter("newpasswordagain");
//		log.info("oldpassword------->" + oldpassword + "   " + "newpassword------->" + newpassword + "    "
//				+ "newpasswordagain---->" + newpasswordagain);
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		CsairReturnValue returnValue = webClient.modifyPassword(oldpassword, newpassword, newpasswordagain);
		return returnValue;
	}

	// /**
	// * 注册页面
	// */
	// @RequestMapping(value="goToRegister",method=RequestMethod.GET)
	// public ModelAndView goRegisterPage(HttpSession session) {
	// Csair webClient=(CsairImpl) session.getAttribute(Csair.WEBCLIENT);
	// Map<String, Object> map = webClient.preRegister();
	// return new ModelAndView("csair/register1",map);
	// }
	/**
	 * 注册页面
	 */
	@RequestMapping(value = "register", method = RequestMethod.GET)
	public ModelAndView register() {
		return new ModelAndView("csair/register2");
	}

	/**
	 * 注册页面
	 */
	@RequestMapping(value = "preRegister", method = RequestMethod.POST)
	@ResponseBody
	public void preRegister(HttpSession session) {
		log.info("================加载注册页=====================");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		webClient.preRegister();
	}

	/**
	 * 执行注册
	 * 
	 * @param request
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "register", method = RequestMethod.POST)
	@ResponseBody
	public CsairReturnValue register(HttpServletRequest request, HttpSession session) {
		log.info("===================执行注册====================");
		String username = request.getParameter("username");
		String username1 = request.getParameter("username1");
		String ID_type = request.getParameter("ID_type");
//		log.info("ID_type=====>" + ID_type);
		String countryCode = request.getParameter("countryCode");
		String mobile = request.getParameter("mobile");
		String email = request.getParameter("email");
		String ID_number = request.getParameter("ID_number");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		CsairReturnValue returnValue = webClient.register(username, username1, countryCode, mobile, ID_type, ID_number,
				email);
		return returnValue;
	}

	/**
	 * 跳转注册验证码页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "registerYzm", method = RequestMethod.GET)
	public ModelAndView registerYzm() {
		return new ModelAndView("csair/registerYzm2");
	}

	@RequestMapping(value = "submitYzm", method = RequestMethod.POST)
	@ResponseBody
	public CsairReturnValue submitYzm(HttpServletRequest request, HttpSession session) {
		log.info("==================确定注册===================");
		String code = request.getParameter("code");
		Csair webClient = (CsairImpl) session.getAttribute(Csair.WEBCLIENT);
		CsairReturnValue returnValue = webClient.registerAuthCode(code);
		return returnValue;
	}

}
